package com.chinacoal.microservice.impl.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.chinacoal.microservice.api.model.QueryUtaskCatAttrMapParam;
import com.chinacoal.microservice.api.model.entity.UtaskCatAttrMapEntity;
import com.chinacoal.microservice.impl.mapper.UtaskCatAttrMapMapper;
import com.chinacoal.microservice.impl.service.UtaskCatAttrMapService;
import com.chinacoal.microservice.impl.util.UtaskCommonFun;
import com.chinacoal.microservice.util.PrimaryKey;

@Service
public class UtaskCatAttrMapServiceImpl extends ServiceImpl<UtaskCatAttrMapMapper, UtaskCatAttrMapEntity> implements UtaskCatAttrMapService {
    
	@Resource
    private UtaskCatAttrMapMapper utaskCatAttrMapMapper;

	@Override
    @Transactional(isolation=Isolation.READ_COMMITTED,propagation=Propagation.REQUIRED,rollbackFor={Exception.class})
	public void insertUtaskCatAttrMap(UtaskCatAttrMapEntity utaskCatAttrMapEntity) throws Exception {
		
		// 校验
		String catId = utaskCatAttrMapEntity.getCatId();
		String attrId = utaskCatAttrMapEntity.getAttrId();
		Integer count = utaskCatAttrMapMapper.getCatAttrMapCount(null, catId, attrId);
		if(count != null && count > 0) throw new Exception("该待办类型映射已存在，请勿重复添加");
		
		// 执行新增 
		utaskCatAttrMapEntity.setMapId(PrimaryKey.newKey());
		utaskCatAttrMapMapper.insert(utaskCatAttrMapEntity);
	}

	@Override
    @Transactional(isolation=Isolation.READ_COMMITTED,propagation=Propagation.REQUIRED,rollbackFor={Exception.class})
	public void updateUtaskCatAttrMap(UtaskCatAttrMapEntity utaskCatAttrMapEntity) throws Exception {
		
		// 执行修改
		utaskCatAttrMapMapper.updateById(utaskCatAttrMapEntity);
		
		// 校验
		String catId = utaskCatAttrMapEntity.getCatId();
		String attrId = utaskCatAttrMapEntity.getAttrId();
		Integer count = utaskCatAttrMapMapper.getCatAttrMapCount(null, catId, attrId);
		if(count != null && count > 1) throw new Exception("该待办类型映射已存在，请检查");
	}

	@Override
    @Transactional(isolation=Isolation.READ_COMMITTED,propagation=Propagation.REQUIRED,rollbackFor={Exception.class})
	public void deleteUtaskCatAttrMap(String mapId) throws Exception {
		
		// 执行删除
		utaskCatAttrMapMapper.deleteById(mapId);
	}

	@Override
	public Page<UtaskCatAttrMapEntity> findList(Integer page, Integer pageSize, QueryUtaskCatAttrMapParam param) throws Exception{
		
    	Page<UtaskCatAttrMapEntity> utaskCatAttrMapPage = UtaskCommonFun.getPage(page, pageSize);
		
		List<UtaskCatAttrMapEntity> utaskCatAttrMapList = utaskCatAttrMapMapper.findList(utaskCatAttrMapPage, param);
		
		return utaskCatAttrMapPage.setRecords(utaskCatAttrMapList);
	}

	@Override
	public List<UtaskCatAttrMapEntity> getCatAttrMap(String sysId, String catId, String attrId) throws Exception{
		return utaskCatAttrMapMapper.getCatAttrMap(sysId, catId, attrId);
	}

	@Override
	public Integer getCatAttrMapCount(String sysId, String catId, String attrId) throws Exception{
		return utaskCatAttrMapMapper.getCatAttrMapCount(sysId, catId, attrId);
	}

}
